package com.netty.serial;

import java.io.File;
import java.io.FileOutputStream;

import com.utils.GzipUtils;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHandler extends ChannelHandlerAdapter {


	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(" server channel active... ");
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		Req req = (Req) msg;
		System.out.println("服务器接收到数据："+req.getId()+","+req.getName()+","+req.getRequestMessage());
		//把文件保存
		byte []data = GzipUtils.ungzip(req.getAttachment());
		String path = System.getProperty("user.dir") + File.separatorChar + "receive" + File.separatorChar + "1.mpp";
		FileOutputStream out = new FileOutputStream(path);
		out.write(data);
		out.close();
		
		Resp resp = new Resp();
		resp.setId(req.getId());
		resp.setName("resp"+req.getId());
		resp.setResponseMessage("回应消息："+req.getId());
		ctx.writeAndFlush(resp);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable t) throws Exception {
		ctx.close();
	}




}
